package org.baojie.raft;

import org.baojie.raft.box.Box;
import org.baojie.raft.group.RaftGroup;
import org.baojie.raft.node.NodeName;
import org.baojie.raft.store.NodeStore;
import org.baojie.raft.store.RaftNodeStore;

import java.io.File;

public class RaftCluster {

    //       4497
    //       6168
    //       5050
    //       2848
    //       1563
    //       7986

    private final NodeStore store;

    private final RaftNode node;

    private final Box box;

    public RaftCluster(File file, RaftGroup group) {
        NodeName name = group.localName();
        this.store = new RaftNodeStore(file, name);
        try {
            store.open();
        } catch (Throwable t) {
            throw new Error();
        }
        this.box = new RaftBox(name, store, group);
        this.node = new RaftNode(box);
    }

    public void start() {
        box.start();
        node.start();
    }

    public void stop() {
        box.stop();
        node.stop();
    }

}
